/**
 * Copyright &copy; 2013-2015 山东易科德软件有限公司 All rights reserved.
 */
package cn.net.ecode.modules.sys.utils;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import cn.net.ecode.common.mapper.JsonMapper;
import cn.net.ecode.common.utils.CacheUtils;
import cn.net.ecode.common.utils.SpringContextHolder;
import cn.net.ecode.modules.sys.dao.UserDictDao;
import cn.net.ecode.modules.sys.dao.UserDictTypeDao;
import cn.net.ecode.modules.sys.entity.UserDict;
import cn.net.ecode.modules.sys.entity.UserDictType;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

/**
 * 字典工具类
 * @author ThinkGem
 * @version 2015-10-29
 */
public class UserDictUtils {
	
	private static UserDictDao dictDao = SpringContextHolder.getBean(UserDictDao.class);
	private static UserDictTypeDao dictTypeDao = SpringContextHolder.getBean(UserDictTypeDao.class);

	public static final String CACHE_DICT_MAP = "userDictMap";
	
	/**
	 * 获取字典标签
	 * @param value
	 * @param type
	 * @param defaultValue
	 * @return
	 */
	public static String getUserDictLabel(String value, String type, String defaultValue){
		if (StringUtils.isNotBlank(type) && StringUtils.isNotBlank(value)){
			for (UserDict dict : getUserDictList(type)){
				if (type.equals(dict.getDictTypeCode()) && value.equals(dict.getDictValue())){
					return dict.getDictName();
				}
			}
		}
		return defaultValue;
	}
	
	/**
	 * 获取字典标签（多个值）
	 * @param values
	 * @param type
	 * @param defaultValue
	 * @return
	 */
	public static String getUserDictLabels(String values, String type, String defaultValue){
		if (StringUtils.isNotBlank(type) && StringUtils.isNotBlank(values)){
			List<String> valueList = Lists.newArrayList();
			for (String value : StringUtils.split(values, ",")){
				valueList.add(getUserDictLabel(value, type, defaultValue));
			}
			return StringUtils.join(valueList, ",");
		}
		return defaultValue;
	}

	/**
	 * 获取字典值
	 * @param label
	 * @param type
	 * @param defaultLabel
	 * @return
	 */
	public static String getUserDictValue(String label, String type, String defaultLabel){
		if (StringUtils.isNotBlank(type) && StringUtils.isNotBlank(label)){
			for (UserDict dict : getUserDictList(type)){
				if (type.equals(dict.getDictTypeCode()) && label.equals(dict.getDictName())){
					return dict.getDictValue();
				}
			}
		}
		return defaultLabel;
	}

	/**
	 * 返回字典列表
	 * @param type
	 * @return
	 */
	public static List<UserDict> getUserDictList(String type){
		@SuppressWarnings("unchecked")
		Map<String, List<UserDict>> dictMap = (Map<String, List<UserDict>>)CorpUtils.getCache(CACHE_DICT_MAP);
		if (dictMap==null){
			dictMap = Maps.newHashMap();
			for (UserDictType dictType : dictTypeDao.findList(new UserDictType())){
				List<UserDict> dictList = Lists.newArrayList();
				dictMap.put(dictType.getDictTypeCode(), dictList);
			}
			for (UserDict dict : dictDao.findList(new UserDict())){
				List<UserDict> dictList = dictMap.get(dict.getDictTypeCode());
				if (dictList != null){
					dictList.add(dict);
				}else{
					dictMap.put(dict.getDictTypeCode(), Lists.newArrayList(dict));
				}
			}
			CorpUtils.putCache(CACHE_DICT_MAP, dictMap);
		}
		List<UserDict> dictList = dictMap.get(type);
		if (dictList == null){
			dictList = Lists.newArrayList();
		}
		return dictList;
	}
	
	/**
	 * 返回字典列表（JSON）
	 * @param type
	 * @return
	 */
	public static String getUserDictListJson(String type){
		return JsonMapper.toJsonString(getUserDictList(type));
	}
	
	/**
	 * 清理缓存
	 */
	public static void clearUserDictCache(){
		CacheUtils.remove(CACHE_DICT_MAP);
	}
	
}
